package com.actor.develop_helper.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.actor.develop_helper.R;
import com.actor.myandroidframework.utils.LogUtils;

/**
 * description: 屏幕朝向
 * @author    : ldf
 * @date      : 2024/12/20 on 17:14
 */
//public class ScreenOrientationActivity extends BaseActivity<ActivityScreenOrientationBinding> {
public class ScreenOrientationActivity extends AppCompatActivity {

    private boolean isGetVersion = false;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        LogUtils.error("attachBaseContext()");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //-1
        int requestedOrientation = getRequestedOrientation();
        LogUtils.errorFormat("onCreate()前, requestedOrientation=%d", requestedOrientation);
        super.onCreate(savedInstanceState);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_screen_orientation);
        setTitle("屏幕朝向");
        LogUtils.errorFormat("onCreate(): savedInstanceState=%s", savedInstanceState);

        //代码设置横屏
        findViewById(R.id.btn_landscape).setOnClickListener(v -> {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        });
        //代码设置竖屏
        findViewById(R.id.btn_portrait).setOnClickListener(v -> {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        LogUtils.errorFormat("onCreate(): savedInstanceState=%s, persistentState=%s", savedInstanceState, persistentState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.error("onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.error("onResume()");
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        LogUtils.error("onAttachedToWindow()");
    }



    ///////////////////////////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////////////////////////
    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.error("onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.error("onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.error("onDestroy()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.error("onRestart()");
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        LogUtils.error("onDetachedFromWindow()");
    }

    //屏幕旋转时保存数据
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong("currentTimeMillis", System.currentTimeMillis());
        LogUtils.errorFormat("onSaveInstanceState(): outState=%s", outState);
    }

    //屏幕旋转后获取数据
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        long currentTimeMillis = savedInstanceState.getLong("currentTimeMillis");
        LogUtils.errorFormat("onRestoreInstanceState(): savedInstanceState=%s", savedInstanceState);
        LogUtils.errorFormat("currentTimeMillis=%d", currentTimeMillis);
    }



    ///////////////////////////////////////////////////////////////////////////
    //
    ///////////////////////////////////////////////////////////////////////////
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtils.errorFormat("onNewIntent(): intent=%s", intent);
    }

    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
        LogUtils.errorFormat("onActivityReenter(): resultCode=%d, data=%s", resultCode, data);
    }


    /**
     *
     * @param newConfig The new device configuration.
     */
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        /**
         * {1.0 ?mcc?mnc [zh_CN] ldltr sw360dp w780dp h332dp 480dpi nrml long hdr widecg land finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 2340, 1080) mAppBounds=Rect(0, 0 - 2340, 1080) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_270} s.3 themeId=2 nightModeLevel = -1}
         * fontScale: 1.0f
         * ?mcc     : mcc=0
         * ?mnc     : mcc=0
         * [zh_CN]  : mLocaleList=[zh_CN]
         * ldltr    : layoutDir={@link Configuration.SCREENLAYOUT_LAYOUTDIR_LTR}
         * sw360dp  : smallestScreenWidthDp=360
         * w780dp   : screenWidthDp=780
         * h332dp   : screenHeightDp=332
         * 480dpi   : densityDpi=480
         * nrml     : screenLayout&SCREENLAYOUT_SIZE_MASK={@link Configuration.SCREENLAYOUT_SIZE_NORMAL}
         * long     : screenLayout&SCREENLAYOUT_LONG_MASK={@link Configuration.SCREENLAYOUT_LONG_YES}
         * hdr      : colorMode &COLOR_MODE_HDR_MASK={@link Configuration.COLOR_MODE_HDR_YES}
         * widecg   : colorMode &COLOR_MODE_WIDE_COLOR_GAMUT_MASK={@link Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES}
         * land     : orientation={@link Configuration.ORIENTATION_LANDSCAPE}
         *          : uiMode&UI_MODE_TYPE_MASK={@link Configuration.UI_MODE_TYPE_NORMAL}
         *          : uiMode&UI_MODE_NIGHT_MASK={@link Configuration.UI_MODE_NIGHT_NO}
         * finger   : touchscreen={@link Configuration.TOUCHSCREEN_FINGER}
         * -keyb    : keyboard={@link Configuration.KEYBOARD_NOKEYS}
         * /v       : keyboardHidden={@link Configuration.KEYBOARDHIDDEN_NO}
         * /h       : hardKeyboardHidden={@link Configuration.HARDKEYBOARDHIDDEN_YES}
         * -nav     : navigation={@link Configuration.NAVIGATION_NONAV}
         * /h       : navigationHidden={@link Configuration.NAVIGATIONHIDDEN_YES}
         * winConfig={ mBounds=Rect(0, 0 - 2340, 1080) mAppBounds=Rect(0, 0 - 2340, 1080) mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_270}
         *          : windowConfiguration=↑
         *          : assetsSeq=0
         * s.3      : seq=3
         * themeId=2 nightModeLevel = -1
         */
        LogUtils.errorFormat("onConfigurationChanged(): newConfig=%s", newConfig.toString());

        // 检查屏幕朝向是否改变
        int orientation0 = getResources().getConfiguration().orientation;//和下方一样的值
        int orientation = newConfig.orientation;
        //orientation0=2, orientation=2
        LogUtils.errorFormat("orientation0=%d, orientation=%d", orientation0, orientation);

        int keyboard = newConfig.keyboard;
        //Configuration.KEYBOARDHIDDEN_YES, Configuration.KEYBOARDHIDDEN_NO
        int keyboardHidden = newConfig.keyboardHidden;
        int hardKeyboardHidden = newConfig.hardKeyboardHidden;
        //keyboard=1, keyboardHidden=1, hardKeyboardHidden=2
        LogUtils.errorFormat("keyboard=%d, keyboardHidden=%d, hardKeyboardHidden=%d", keyboard, keyboardHidden, hardKeyboardHidden);

        int screenWidthDp = newConfig.screenWidthDp;
        int screenHeightDp = newConfig.screenHeightDp;
        //screenWidthDp=780, screenHeightDp=332
        LogUtils.errorFormat("screenWidthDp=%d, screenHeightDp=%d", screenWidthDp, screenHeightDp);

        switch (orientation) {
            case Configuration.ORIENTATION_UNDEFINED:   // 未定义?
                LogUtils.errorFormat("onConfigurationChanged(): orientation=%d: %s", orientation, "未指定");
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                LogUtils.errorFormat("onConfigurationChanged(): orientation=%d: %s", orientation, "竖屏模式");
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                LogUtils.errorFormat("onConfigurationChanged(): orientation=%d: %s", orientation, "横屏模式");
                break;
            case Configuration.ORIENTATION_SQUARE:
                LogUtils.errorFormat("onConfigurationChanged(): orientation=%d: %s", orientation, "正方形");
                break;
            default:
                LogUtils.errorFormat("onConfigurationChanged(): orientation=%d: %s", orientation, "方向???");
                break;
        }

        //if横屏&竖屏的layout不一样, 这儿需要重新设置layout
//        setContentView(R.layout.activity_screen_orientation);
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        super.setRequestedOrientation(requestedOrientation);
        LogUtils.errorFormat("setRequestedOrientation(): requestedOrientation=%d", requestedOrientation);
    }
}